//
//  JCLNavTitleButton.swift
//  WeiBo
//
//  Created by DDC on 2017/9/14.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit

class JCLNavTitleButton: UIButton {

    init(title:String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            //用户名为空的时候
            setTitle("首页", for: [])
            
        }else {
            setTitle(title!, for: [])
            //设置箭头图像
            setImage(UIImage.init(named: "navigationbar_arrow_down"), for: [])
            setImage(UIImage.init(named: "navigationbar_arrow_up"), for: .selected)
        }
        //设置大小
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
        sizeToFit()
        
    }
    
    required init?(coder aDecoder: NSCoder) { 
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        
        super.layoutSubviews()
       
        guard let titleLabel = titleLabel,let imageView = imageView else {
            return
        }
        
        //将文字与图像交换位置
//        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
//        
//        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        titleLabel.frame = CGRect(x: 0, y: 0, width: titleLabel.frame.size.width, height: titleLabel.frame.size.height)
        imageView.frame = CGRect(x: titleLabel.frame.size.width + 5, y: 8, width: imageView.frame.size.width, height:imageView.frame.size.height)
        
    }

}
